home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / ead / ead11.dms / ead11.adf / Listati / Ing.c < prev    next >
C/C++ Source or Header  |  1989-01-08  |  8KB  |  355 lines

  1. /* :ts=8 bk=0
  2. *******************************************************************
  3.  
  4. * Ing.c - Un divertimento da Workbench - Scritto da Leo L. Schwab *
  5.  
  6. * Programma di pubblico dominio, si prega solo di lasciare i nomi *
  7.  
  8. **** Tradotto ed adattato a Aztec C V3.6 da Luigi R. Callegari ****
  9.  
  10. **********  Compilazione: cc Ing +L (interi a 32 bit)   ***********
  11. **********        Linking : ln -lm32 -lc32 Ing          ***********
  12.  
  13. ******************************************************************/
  14.  
  15. #include <exec/types.h>
  16. #include <intuition/intuition.h>
  17. #include <math.h>
  18.  
  19. #define    DEPTH        2
  20.  
  21. extern void    *OpenLibrary(), *OpenWindow(), *OpenScreen(), *AllocRaster(),
  22.         *GetMsg(), *malloc();
  23. extern long    VBeamPos();
  24.  
  25. struct NewScreen scrdef = {
  26.     0, 0, 0, 0, DEPTH,    /* Dimensioni fissate dopo */
  27.     0, 1,
  28.     HIRES,
  29.     CUSTOMSCREEN,
  30.     NULL,
  31.     NULL,            /*  Titolo fissato dopo */
  32.     NULL, NULL
  33. };
  34.  
  35. struct NewWindow windef = {
  36.     0, 30, 150, 10,
  37.     -1, -1,
  38.     CLOSEWINDOW,
  39.     WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH|SMART_REFRESH|ACTIVATE,
  40.     NULL, NULL,
  41.     (UBYTE *)"Ing!",
  42.     NULL, NULL,
  43.     0, 0, 0, 0,
  44.     WBENCHSCREEN
  45. };
  46.  
  47. struct wlist {
  48.     struct wlist *next, *prev;
  49.     struct BitMap bitmap;
  50.     long sizx, sizy;
  51.     int maxx, maxy;
  52.     int x, y, dx, dy;
  53.     int maxdy;
  54. };
  55.  
  56. struct Screen    *scr, *wb;
  57. struct Window    *win;
  58. struct BitMap    *bm1, bm2, barmap;
  59. struct wlist    *listbase;
  60. void        *IntuitionBase, *GfxBase;
  61.  
  62. void main ()
  63. {
  64.     struct Window        *wbwin;
  65.     struct ViewPort        *svp;
  66.     register struct BitMap    *abm;
  67.     struct BitMap        *wbbm, **vbm;
  68.     register struct wlist    *this = NULL;
  69.     struct wlist        *new;
  70.     long            wide, high, barhigh, wbhigh;
  71.     int            i;
  72.     void            *msg;
  73.  
  74.     openstuff ();
  75.     rnd ((short) -VBeamPos());
  76.     wb = win -> WScreen;    /*  Workbench screen  */
  77.     wbbm = wb -> RastPort.BitMap;
  78.     scrdef.LeftEdge = wb -> LeftEdge;
  79.     scrdef.TopEdge    = wb -> TopEdge;
  80.     scrdef.Width    = wb -> Width;
  81.     scrdef.Height    = wbhigh = wb -> Height;
  82.  
  83.     /*  Scandisce tutte le finestre nel Workbench  */
  84.     for (wbwin = wb -> FirstWindow; wbwin; wbwin = wbwin -> NextWindow) {
  85.         if (wbwin->Flags & BACKDROP && wbwin->Flags & WBENCHWINDOW) {
  86.             scrdef.DefaultTitle = wbwin -> ScreenTitle;
  87.             continue;
  88.         }
  89.  
  90.         if (wbwin -> Height > wbhigh-21)
  91.             continue;    /*  troppo piccola, lasciamo */
  92.         if (wbwin -> Width > wb->Width - 40)
  93.             continue;    /*  troppo larga, lasciamo */
  94.  
  95.         /*  Allocate new entry in list  */
  96.         if (!(new = malloc (sizeof (*new))))
  97.             die ("malloc() non funziona!");
  98.         new -> prev = this;
  99.         new -> next = NULL;
  100.         if (this)
  101.             this -> next = new;
  102.         listbase = new;
  103.  
  104.         /*  Legge informazioni necessarie  */
  105.         wide = new -> sizx = wbwin -> Width;
  106.         high = new -> sizy = wbwin -> Height;
  107.         new -> maxx = wb -> Width - wide;
  108.         new -> maxy = wb -> Height - high;
  109.         new -> x = wbwin -> LeftEdge;
  110.         new -> y = wbwin -> TopEdge;
  111.         new -> dx = new -> dy = 0;
  112.  
  113.         /* Calcolo della max velocita' verticale iniziale.
  114.         Mi ci e' voluto un sacco di tempo per calcolare questa
  115.         formula. Qui a Princeton studio altro...*/
  116.  
  117.         new -> maxdy = sqrt (2 * new->maxy + 0.25) - 0.5 + 1.0;
  118.  
  119.         /*  Crea bitmap e vi copia la finestra  */
  120.  
  121.         InitBitMap (&new -> bitmap, (long) DEPTH, wide, high);
  122.         for (i=0; i<DEPTH; i++)
  123.         if (!(new -> bitmap.Planes[i] = AllocRaster (wide, high)))
  124.             die ("AllocRaster() non funziona!");
  125.  
  126.         WindowToFront (wbwin);    /*  WindowToFront() non funge  */
  127.         Delay (5L);        /*  subito, quindi aspettiamo  */
  128.         BltBitMap
  129.          (wbbm, (long) wbwin -> LeftEdge, (long) wbwin -> TopEdge,
  130.           &new -> bitmap, 0L, 0L,
  131.           wide, high, 0xC0L, 0xffL, NULL);
  132.  
  133.         this = new;
  134.     }
  135.  
  136.     /* Letto e copiato tutte le finestre. Ora c'e' il bello */
  137.  
  138.     scr = OpenScreen (&scrdef);    /*  Clona il WorkBench  */
  139.     bm1 = scr -> ViewPort.RasInfo -> BitMap;
  140.     svp = &scr -> ViewPort;
  141.  
  142.     /* Crea secondo insieme di bitplanes per doppio buffer */
  143.  
  144.     wide = wb -> Width;
  145.     InitBitMap (&bm2, (long) DEPTH, wide, wbhigh);
  146.     for (i=0; i<DEPTH; i++)
  147.         if (!(bm2.Planes[i] = AllocRaster (wide, wbhigh)))
  148.             die ("AllocRaster() 2 ha fallito!");
  149.     BltBitMap
  150.      (bm1, 0L, 0L, &bm2, 0L, 0L, wide, wbhigh, 0xc0L, 0xffL, NULL);
  151.  
  152.     /* Crea bitmap privata per conservare barra di titolo schermo */
  153.  
  154.     barhigh = wb -> BarHeight;
  155.     InitBitMap (&barmap, (long) DEPTH, wide, barhigh);
  156.     for (i=0; i<DEPTH; i++)
  157.         if (!(barmap.Planes[i] = AllocRaster (wide, barhigh)))
  158.             die ("AllocRaster() 3 ha fallito!");
  159.     BltBitMap
  160.      (bm1, 0L, 0L, &barmap, 0L, 0L, wide, barhigh, 0xc0L, 0xffL, NULL);
  161.  
  162.     i = 0;
  163.     vbm = &scr -> ViewPort.RasInfo -> BitMap;
  164.     abm = &bm2;
  165.     while (1) {
  166.  
  167.         /*  Copia barra schermo  */
  168.  
  169.         BltBitMap (&barmap, 0L, 0L,
  170.                abm, 0L, 0L,
  171.                wide, barhigh, 0xc0L, 0xffL, NULL);
  172.  
  173.         /* Pulisce resto schermo */
  174.  
  175.         BltBitMap (abm, 0L, 0L,
  176.                abm, 0L, barhigh,
  177.                wide, wbhigh-barhigh, 0L, 0xffL, NULL);
  178.  
  179.         /* Copia finestre scandite all'indietro */
  180.  
  181.         for (this = listbase; this; this = this -> prev) {
  182.             if ((this -> x += this -> dx) > this -> maxx) {
  183.                 this -> x = this->maxx * 2 - this->x;
  184.                 this -> dx = -this -> dx;
  185.             } else if (this -> x < 0) {
  186.                 this -> x = -this -> x;
  187.                 this -> dx = -this -> dx;
  188.             }
  189.  
  190.             if ((this -> y += this -> dy) > this -> maxy) {
  191.                 this -> y = this -> maxy;
  192.                 this -> dy = -rnd (this->maxdy - 4) - 4;
  193.                 this -> dx = rnd (31) - 15;
  194.             } else
  195.                 this -> dy++;
  196.  
  197.             /*  Copia immagine finestra  */
  198.  
  199.             BltBitMap
  200.              (&this -> bitmap, 0L, 0L,
  201.               abm, (long) this -> x, (long) this -> y,
  202.               this -> sizx, this -> sizy, 0xc0L, 0xffL, NULL);
  203.         }
  204.  
  205.         /* Questa e' la parte intrigante. Ho cercato di mantenere
  206.         al minimo lo hashing, ma in certe fasi lunari e' sempre
  207.         inevitabile */
  208.  
  209.         Forbid ();
  210.         if (i) {    /*  Ping  */
  211.             *vbm = bm1;
  212.             abm = &bm2;
  213.         } else {    /*  Pong  */
  214.             *vbm = &bm2;
  215.             abm = bm1;
  216.         }
  217.         i = !i;
  218.  
  219.         /*  Sorpresa!  Intuition se ne frega di questo! */
  220.  
  221.         ScrollVPort (svp);
  222.         Permit ();
  223.  
  224.         if (msg = GetMsg (win -> UserPort)) {
  225.             ReplyMsg (msg);
  226.             break;
  227.         }
  228.         WaitTOF ();
  229.     }
  230.  
  231.     /*  Rimetti l'originale  */
  232.     Forbid ();
  233.     *vbm = bm1;
  234.     ScrollVPort (svp);    /*  Ping!  */
  235.     Permit ();
  236.  
  237.     closestuff ();
  238. }
  239.  
  240. openstuff ()
  241. {
  242.     if (!(IntuitionBase = OpenLibrary ("intuition.library", 0L))) {
  243.         printf ("Intuition dov'e'???.\n");
  244.         exit (100);
  245.     }
  246.  
  247.     if (!(GfxBase = OpenLibrary ("graphics.library", 0L))) {
  248.         printf ("Le routines grafiche???\n");
  249.         closestuff ();
  250.         exit (100);
  251.     }
  252.  
  253.     if (!(win = OpenWindow (&windef))) {
  254.         printf ("La finestra???\n");
  255.         closestuff ();
  256.         exit (100);
  257.     }
  258. }
  259.  
  260. die (str)
  261. char *str;
  262. {
  263.     puts (str);
  264.     closestuff ();
  265.     exit (100);
  266. }
  267.  
  268. closestuff ()
  269. {
  270.     register int i;
  271.  
  272.     for (i=0; i<DEPTH; i++) {
  273.         if (barmap.Planes[i])
  274.             FreeRaster (barmap.Planes[i],
  275.                     (long) wb->Width, (long) wb->BarHeight);
  276.         if (bm2.Planes[i])
  277.             FreeRaster (bm2.Planes[i],
  278.                      (long) wb->Width, (long) wb->Height);
  279.     }
  280.  
  281.     if (listbase)        freelist ();
  282.     if (scr)        CloseScreen (scr);
  283.     if (win)        CloseWindow (win);
  284.     if (GfxBase)        CloseLibrary (GfxBase);
  285.     if (IntuitionBase)    CloseLibrary (IntuitionBase);
  286. }
  287.  
  288. freelist()
  289. {
  290.     register struct wlist *this = listbase, *tmp;
  291.     register int i;
  292.  
  293.     while (this) {
  294.         for (i=0; i<DEPTH; i++)
  295.             if (this -> bitmap.Planes[i])
  296.                 FreeRaster (this -> bitmap.Planes[i],
  297.                         this -> sizx, this -> sizy);
  298.         tmp = this;
  299.         this = this -> prev;
  300.         free (tmp);
  301.     }
  302. }
  303.  
  304.  
  305.  
  306. #asm
  307. *\
  308. *  :ts=8 bk=0
  309. * Un altro generatore di numeri casuali!!! By Leo Schwab
  310. * Grazie a Sam Dicker per l'idea.
  311. * Per assembler Metacomco
  312. *
  313. * Chiamata:
  314. *  long rnd (range);
  315. *  long range;
  316. *
  317. * Assemblaggio con Metacomco
  318. *  df1:c/assem rnd.s -o rnd.o
  319. */
  320.  
  321.         blanks off
  322.         section code
  323.         xdef    _rnd
  324.  
  325. _rnd        lea    rndseed,a0    ;Indirizzo deme
  326.         move.l    4(sp),d1    ;Gamma argomento
  327.         tst.l    d1
  328.         ble.s    setseed        ;Seme di reset
  329.  
  330.  
  331.         move.l    (a0),d0        ;Legge seme
  332.         ADD.L   D0,D0
  333.         BHI.S   over
  334.         EORI.L  #$1D872B41,D0
  335. over
  336.         move.l    d0,(a0)        ;Salva nuovo seme
  337.         andi.l    #$ffff,d0    ;Forza in una word
  338.         andi.l    #$ffff,d1
  339.         divu    d1,d0        ;Divide per gamma
  340.         swap    d0        ; resto (modulo)
  341.         ext.l    d0
  342.         rts
  343.  
  344. setseed        neg.l    d1        ;Probabilmente non necessario
  345.         move.l    d1,(a0)
  346.         rts
  347.  
  348.         section data
  349. rndseed        dc.l    0
  350.         section code
  351.  
  352. #endasm
  353.  
  354.